﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using VirtualPress.Portal.WebLogic.Authentication;
using VirtualPress.Portal.Web.Areas.WorkSpace.Models;
using VirtualPress.Portal.WebLogic;
using VirtualPress.Portal.Biz;
using VirtualPress.Infrastructure.Entities.Entity;
using VirtualPress.Infrastructure.Entities.Enum;
using System.Linq.Expressions;
using VirtualPress.Infrastructure.Entities.Repository;
using System.IO;

namespace VirtualPress.Portal.Web.Areas.WorkSpace.Controllers
{
#if !DEBUG
    [ErrorHandle]
#endif
    [Auth(UserRole.Admin)]
    [WorkSpace(UserRole.Admin)]
    public class PressController : Controller
    {
        public ActionResult Index()
        {
            return View();
        }
        public ActionResult JournalManage(int page =0)
        {
            var journals = new ItemListBox<Periodical>(JournalBiz.GetAllJournal(c=>c.IsActive, page, 20));
            journals.BuildPage(JournalBiz.GetAllJournalForPage(c => c.IsActive), page, 20,
                new PageParameter() { 
                    IsActive=false,
                    Param_page="page",
                    Style="workspace"
                });
            ViewBag.Journals = journals;
            return View();
        }
        public ActionResult Edit(int id)
        {
            var journal = new Periodical().Find(id.ToString());
            ViewBag.Journal = journal;
            return View();
        }
        [HttpPost]
        [ValidateInput(false)]
        public ActionResult Edit(int Id, NewJournalModel model)
        {
            var journale = new Periodical().Find(Id.ToString());
            if (journale != null)
            {
                string basedir = "/content/journal/" + journale.Name + "/converimage/";
                var file = Request.Files["CoverImg"];
                if (file != null && file.ContentLength > 0)
                {
                    string extendname = new FileInfo(file.FileName).Extension;
                    string fileName = file.FileName.Replace(extendname, "");
                    if (!System.IO.Directory.Exists(HttpContext.Server.MapPath(basedir)))
                    {
                        System.IO.Directory.CreateDirectory(HttpContext.Server.MapPath(basedir));
                    }
                    file.SaveAs(HttpContext.Server.MapPath(basedir + fileName + extendname));
                    journale.CoverImg = basedir + fileName + extendname;
                }
                journale.Detail = model.Detail;
                journale.Admin = model.Admin;
                journale.BSGW = model.BSGW;
                journale.BSGW_EN = model.BSGW_EN;
                journale.CallForPaper = model.CallForPaper;
                journale.CallForPaper_EN = model.CallForPaper_EN;
                journale.CBDW = model.CBDW;
                journale.CBDW_EN = model.CBDW_EN;
                journale.Editer_EN = model.Editer_EN;
                journale.Editor = model.Editor;
                journale.Email = model.Email;
                journale.FoundTime = model.FoundTime;
                journale.GJBZLXCBWH = model.GJBZLXCBWH;
                journale.GJGW = model.GJGW;
                journale.GJGW_EN = model.GJGW;
                journale.GNTYLXCBWH = model.GNTYLXCBWH;
                journale.IndType = (Infrastructure.Entities.Enum.Profession)model.IndType;
                journale.Introduction = model.Introduction;
                journale.Introduction_EN = model.Introduction_EN;
                journale.ISSN_Online = model.ISSN_Online;
                journale.ISSN_Print = model.ISSN_Print;
                journale.Name = model.Name;
                journale.Name_EN = model.Name_EN;
                journale.Press_Id = model.Press_Id;
                journale.PubRate =(PubRate)model.PubRate;
                journale.PubTime = model.PubTime;
                journale.Range = model.Range;
                journale.RegTime = model.RegTime;
                journale.Retrieval = model.Retrieval;
                journale.XBDW = model.XBDW;
                journale.XBDW_EN = model.XBDW_EN;
                journale.YFDH = model.YFDH;
                journale.ZBDW = model.ZBDW;
                journale.ZBDW_EN = model.ZBDW_EN;
                journale.ZCDW = model.ZCDW;
                journale.ZCDW_EN = model.ZCDW_EN;
                journale.ZGDW = model.ZGDW;
                journale.ZGDW_EN = model.ZGDW_EN;
                if(journale.PostModify()!=null)
                {
                    return Redirect("/WorkSpace/Home/ServerMessage?c=press&a=edit&s=1");
                }
            }
            return Redirect("/WorkSpace/Home/ServerMessage?c=press&a=edit&s=0");
        }
        public ActionResult EnableJournal(int id)
        {
            if (JournalBiz.Enable(id, 1))
            {
                return Redirect("/WorkSpace/Home/ServerMessage?c=press&a=enablejournal&s=1");
            }
            else
            {
                return Redirect("/WorkSpace/Home/ServerMessage?c=press&a=enablejournal&s=0");
            }
        }
        public ActionResult DisableJournal(int id)
        {
            if (JournalBiz.Enable(id, 0))
            {
                return Redirect("/WorkSpace/Home/ServerMessage?c=press&a=disablejournal&s=1");
            }
            else
            {
                return Redirect("/WorkSpace/Home/ServerMessage?c=press&a=disenablejournal&s=0");
            }
        }
        public ActionResult DeleteJournal(int id)
        {
            if (JournalBiz.Delete(id))
            {
                return Redirect("/WorkSpace/Home/ServerMessage?c=press&a=deletejournal&s=1");
            }
            else
            {
                return Redirect("/WorkSpace/Home/ServerMessage?c=press&a=deletejournal&s=0");
            }
        }
        public ActionResult JournalReq(int page =0)
        {
            var journals = new ItemListBox<Periodical>(JournalBiz.GetAllJournal(c => !c.IsActive, page, 20));
            journals.BuildPage(JournalBiz.GetAllJournalForPage(c => !c.IsActive), page, 20,
               new PageParameter()
               {
                   IsActive = false,
                   Param_page = "page",
                   Style = "workspace"
               });
            ViewBag.Journals = journals; 
            return View();
        }
        public ActionResult PushOne()
        {
            return View();
        }
        [HttpPost]
        [ValidateInput(false)]
        public ActionResult PushOne(NewJournalModel model)
        {
            if (JournalBiz.SaveNewJournal(new Infrastructure.Entities.Entity.Periodical()
            {
                Type = (JournalType)model.Type,
                BSGW = model.BSGW,
                BSGW_EN = model.BSGW_EN,
                CallForPaper = model.CallForPaper,
                CallForPaper_EN = model.CallForPaper_EN,
                CBDW = model.CBDW,
                CBDW_EN = model.CBDW_EN,
                DownloadNumber = 1,
                Editer_EN = model.Editer_EN,
                Editor = model.Editor,
                Email = model.Email,
                FoundTime = model.FoundTime,
                GJBZLXCBWH = model.GJBZLXCBWH,
                GJGW = model.GJGW,
                GJGW_EN = model.GJGW_EN,
                GNTYLXCBWH = model.GNTYLXCBWH,
                IndType = (Infrastructure.Entities.Enum.Profession)model.IndType,
                Introduction = model.Introduction,
                Introduction_EN = model.Introduction_EN,
                IsActive = false,
                ISSN_Online = model.ISSN_Online,
                ISSN_Print = model.ISSN_Print,
                Manuscriptcode = "",
                Name = model.Name,
                Name_EN = model.Name_EN,
                ShortName = model.ShortName,
                Press_Id = 0,
                PubRate = (PubRate)model.PubRate,
                PubTime = model.PubTime,
                Range = model.Range,
                RegTime = DateTime.Now,
                Retrieval = model.Retrieval,
                VisitNumber = 1,
                XBDW = model.XBDW,
                XBDW_EN = model.XBDW_EN,
                YFDH = model.YFDH,
                ZBDW = model.ZBDW,
                ZBDW_EN = model.ZBDW_EN,
                ZCDW = model.ZCDW,
                ZCDW_EN = model.ZCDW_EN,
                ZGDW = model.ZGDW,
                ZGDW_EN = model.ZGDW_EN,
                Detail=model.Detail,
                Admin = model.ShortName + "@csaep.org",
                IsNew=true
            }))
            {
                return Redirect("/WorkSpace/Press/JournalReq");
            }
            return Redirect("/WorkSpace/Home/ServerMessage?c=press&a=pushone&s=0");
        }
        public ActionResult News(int page= 0)
        {
            var data = new ItemListBox<News>(CommonBiz.GetAllNews(NewsType.SiteNews,0, OrganizationType.System, page, 20));
            data.BuildPage(CommonBiz.GetAllNewsForPage(0, OrganizationType.System, NewsType.SiteNews),
                page,
                20,
                 new PageParameter()
                 {
                     IsActive = false,
                     Param_page = "page",
                     Style = "workspace"
                 });
            ViewBag.News = data;
            return View();
        }
        public ActionResult AddNews()
        {
            return View();
        }
        [ValidateInput(false)]
        [HttpPost]
        public ActionResult AddNews(string Title, string DisplayEditor, string Form, string Content)
        {
            CommonBiz.SaveNews(new News()
            {
                Content = Content,
                CreateDate = DateTime.Now,
                DisplayEditor = DisplayEditor,
                Editor = HttpContext.User.Identity.Name,
                Form = Form,
                Type = NewsType.SiteNews,
                Title = Title,
                OrgId=0,
                OrgType= OrganizationType.System
            });
            return Redirect("/WorkSpace/Press/News");
        }
        public ActionResult NewsEdit(int Id)
        {
            ViewBag.Data = new News().Find(Id.ToString());
            return View();
        }
        [ValidateInput(false)]
        [HttpPost]
        public ActionResult NewsEdit(int Id, string Title, string DisplayEditor, string Form, string Content)
        {
            var data = new News().Find(Id.ToString());
            if (data != null)
            {
                data.Title = Title;
                data.DisplayEditor = DisplayEditor;
                data.Form = Form;
                data.Content = Content;
                data.PostModify();
            }
            return Redirect("/WorkSpace/Press/News");
        }
        public ActionResult NewsCancel(int Id)
        {
            var data = new News().Find(Id.ToString());
            if (data != null)
            {
                data.PostDelete();
            }
            return Redirect("/WorkSpace/Press/News");
        }
        public ActionResult Authors()
        {
            return View();
        }
        public ActionResult AuthorEdit()
        {
            return View();
        }
        public ActionResult EditorialBoards()
        {
            return View();
        }
        public ActionResult EditorialBoardEdit()
        {
            return View();
        }
        public ActionResult Experts()
        {
            return View();
        }
        public ActionResult ExpertEdit()
        {
            return View();
        }
        public ActionResult Editors()
        {
            return View();
        }
        public ActionResult EditorEdit()
        {
            return View();
        }
        public ActionResult Article(int group, int page=0)
        {
             var groupmap = new
            {
                最新 = new List<int>() { 0 },
                待分配 = new List<int>() { 2, 4, 6, 8, 10 },
                专审 = new List<int>() { 3, 5 },
                终审 = new List<int>() { 7, 9 },
                录用 = new List<int>() { 30, 40 },
                拒绝 = new List<int>() { 99 },
                缴费 = new List<int>() { 1, 20, 80 }
            };
            Expression<Func<T_Article, bool>> where = null;
            if (group == 9)
            {
                where = c => true;
            }
            else
            {
                List<int> contain = new List<int>();
                switch (group)
                {
                    default:
                    case 0:
                        contain = groupmap.最新;
                        break;
                    case 1:
                        contain = groupmap.待分配;
                        break;
                    case 2:
                        contain = groupmap.专审;
                        break;
                    case 3:
                        contain = groupmap.终审;
                        break;
                    case 4:
                        contain = groupmap.录用;
                        break;
                    case 5:
                        contain = groupmap.拒绝;
                        break;
                    case 6:
                        contain = groupmap.缴费;
                        break;
                }
                where = c => contain.Contains(c.Status) && !c.IsDelete;
            }
            List<Article> data = JournalBiz.GetAllArticle(where,page,20);
            ItemListBox<Article> List = new ItemListBox<Article>(data)
                .BuildPage(JournalBiz.GetAllArticleForPage(where), page, 20,
                new PageParameter()
                {
                    Style = "workspace"
                });
            ViewBag.Data = List;
            return View();
        }
        public ActionResult ArticleDetail(int id)
        {
            var article = new Article().Find(c => c.Id == id);
            var author = new ArticleAuthor().FindAll(c => c.ManuscriptId == article.Id);
            var journal = new Periodical().Find(article.Peri_Id.ToString());
            //作者信息
            ViewBag.ArticleAuthor = author;
            //稿件
            ViewBag.Article = article;
            //期刊
            ViewBag.Journal = journal;
            //附件
            ViewBag.Attachment = new Appendix().FindAll(c => c.Type == (int)AttachmentType.Manuscript && c.RaletionId == article.Id);
            //稿件历史
            ViewBag.History = JournalBiz.GetAllArticleHistory(article.Id);
            //审稿记录
            ViewBag.AuditingRecord = JournalBiz.GetAllAuditingRecord(article.Id);
            return View();
        }
        public ActionResult TobeDistributed()
        {
            return View();
        }
        public ActionResult Assign()
        {
            return View();
        }
        public ActionResult Books(int page= 0)
        {
            List<Book> data = CommonBiz.GetAllBook(page, 10);
            ViewBag.Data = new ItemListBox<Book>(data)
                .BuildPage(new Book().Count(c => true), page, 10,
                new PageParameter()
                {
                    IsActive = false,
                    Style = "workspaceS"
                });
            return View();
        }
        public ActionResult NewBook()
        {
            return View();
        }
        [HttpPost]
        [ValidateInput(false)]
        public ActionResult NewBook(BookModel model)
        {
            if (model != null)
            {
                CommonBiz.NewBook(new Book()
                {
                    Authors = model.Authors,
                    BC = model.BC,
                    Characters = model.Characters,
                    CYZ = model.CYZ,
                    FXFW = model.FXFW,
                    Introduction = model.Introduction,
                    ISBN = model.ISBN,
                    KB = model.KB,
                    Name = model.Name,
                    Pages = model.Pages,
                    Press = model.Press,
                    Price = model.Price,
                    Buy = model.Buy,
                    Translator = model.Translator,
                    YC = model.YC,
                    PubDate = model.PubDate,
                    PrintDate = model.PrintDate,
                    Pack = model.Pack,
                    Content = model.Content,
                    AboutAuthor = model.AboutAuthor,
                    Comments = model.Comments
                }, "fileControll");
            }
            return RedirectToAction("Books");
        }
        public ActionResult BookEdit(int id)
        {
            var book = new Book().Find(id.ToString());
            if (book == null)
            {
                return RedirectToAction("Books");
            }
            ViewBag.Book = book;
            return View();
        }
        [HttpPost]
        [ValidateInput(false)]
        public ActionResult BookEdit(BookModel model)
        {
            if (model != null)
            {
                CommonBiz.EditBookItem(new Book()
                {
                    Authors = model.Authors,
                    BC = model.BC,
                    Characters = model.Characters,
                    CYZ = model.CYZ,
                    FXFW = model.FXFW,
                    Id = model.Id,
                    Introduction = model.Introduction,
                    Buy = model.Buy,
                    ISBN = model.ISBN,
                    KB = model.KB,
                    Name = model.Name,
                    Pages = model.Pages,
                    Press = model.Press,
                    Price = model.Price,
                    Translator = model.Translator,
                    PubDate = model.PubDate,
                    PrintDate = model.PrintDate,
                    Pack = model.Pack,
                    Content = model.Content,
                    AboutAuthor=model.AboutAuthor,
                    Comments=model.Comments,
                    YC = model.YC
                }, "fileControll");
            }
            return RedirectToAction("Books");
        }
        public ActionResult deletebook(int id)
        {
            CommonBiz.DeleteBookItem(id);
            return RedirectToAction("Books");
        }
        public ActionResult PublishApply(int page=0)
        {
            List<PublishApply> list = CommonBiz.GetAllPublishApply(page, 10);
            ViewBag.Data = new ItemListBox<PublishApply>(list).BuildPage(
                CommonBiz.GetPageOfPublishApply(), page, 10,
                new PageParameter()
                {
                    IsActive = false,
                    Param_page = "page",
                    Style = "workspace"
                });
            return View();
        }
        public ActionResult PublishApplyEdit(int id) 
        {
            ViewBag.Data = new PublishApply().Find(id.ToString());
            return View();
        }
        [HttpPost]
        public ActionResult PublishApplyEdit(int id, string name, string authors, string field,
            string way, string phone, string contact,
            string mail, string introduction)
        {
            var apply = new PublishApply().Find(id.ToString());
            apply.Name = name;
            apply.Authors = authors;
            apply.Field = field;
            apply.Way = way;
            apply.Phone = phone;
            apply.Contact = contact;
            apply.Mail = mail;
            apply.Introduction = introduction;
            ViewBag.Data = CommonBiz.EditPublishApply(apply);
            return View();
        }
        public ActionResult deleltepublishapply(int id)
        {
            CommonBiz.DeletePublishApply(id);
            return RedirectToAction("PublishApply");
        }

        #region Meetings
        public ActionResult MeetingsApply(int page = 0)
        {
            List<Meeting> meetings = null;
            int pageCount = 0;
            //全部
            meetings = MeetingBiz.GetAllMeeting(9, page, 20);
            pageCount = MeetingBiz.GetAllMeetingForPage(9);

            ViewBag.Meeting = new ItemListBox<Meeting>(meetings)
                .BuildPage(pageCount,
                page, 20, new PageParameter() { Style = "workspace" });
            return View();
        }
        public ActionResult NewMeeting()
        {
            List<String> gorupName = MeetingBiz.GetMeetingGroupName();
            ViewBag.GroupName = gorupName;
            return View();
        }
        [HttpPost]
        [ValidateInput(false)]
        public ActionResult NewMeeting(string Name,string Name_EN, string ShortName,string Keyword,string Description,
            string GroupName, int IndType, string Email, string ZBDW, string Logo, string Css)
        {
            if (JournalBiz.ShortNameIsExists(ShortName, 2))
            {
                MeetingBiz.NewMeeting(new Meeting()
                {
                    Admin = ShortName + "@csaep.org",
                    isActive = false,
                    Email = Email,
                    IndType = (Profession)IndType,
                    ZBDW=ZBDW,
                    Name = Name,
                    Name_EN=Name_EN,
                    Keyword=Keyword,
                    Description=Description,
                    ShortName = ShortName,
                    isInstall=false,
                    GroupName = GroupName,
                    Css=Css,
                    Logo=Logo
                });
                return Redirect("/WorkSpace/Home/ServerMessage?c=press&a=newmeeting&s=1");
            }
            return Redirect("/WorkSpace/Home/ServerMessage?c=press&a=newmeeting&s=0");
        }
        
        public ActionResult EditForMeeting(int id)
        {
            ViewBag.Meeting = new Meeting().Find(id.ToString());
            return View();
        }
        [HttpPost]
        [ValidateInput(false)]
        public ActionResult EditForMeeting(int id, string Name, string ShortName, int IndType, string Email,
            string ZBDW, string Logo, string Css)
        {
            var meeting = new Meeting().Find(id.ToString());
            meeting.Css = Css;
            meeting.IndType = (Profession)IndType;
            meeting.Email = Email;
            meeting.ZBDW = ZBDW;
            MeetingBiz.EditMeeting(meeting);
            return Redirect("/WorkSpace/Home/ServerMessage?c=press&a=editformeeting&s=1");
        }
        public ActionResult EnableMeeting(int id)
        {
            MeetingBiz.EnableMeeting(id);
            return RedirectToAction("MeetingsApply");
        }
        public ActionResult DisableMeeting(int id)
        {
            MeetingBiz.DisableMeeting(id);
            return RedirectToAction("MeetingsApply");
        }
        public ActionResult DeleteMeeting(int id)
        {
            MeetingBiz.DeleteMeeting(id);
            return RedirectToAction("MeetingsApply");
        }
        #endregion
    }
}
